/*
 * LogUtils
 * Copyright (c) 2019 taisu Inc., All rights reserved.
 * This software is the confidential and proprietary information of
 * taisu Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with tysu.com.cn.
 */

package com.ymk.base.utlis;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ymk.base.cmd.CmdSign;
import com.ymk.core.constants.SysConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

/**
 * 日志优化
 *
 * @author laborc
 * @date 2019-03-25 1:56 PM
 * Revision History
 * Date      		Programmer       Notes
 * 2019-03-25   	 laborc		     Initial
 */
public class LogUtils {
    private static final String LOG_GROUP_STAT = "Stat";
    private static final String LOG_GROUP_AUDIT = "Audit";
    private static final String LOG_GROUP_EX = "EX";

    public static final String LOG_GROUP_STAT_CMD = "Cmd";
    private static final String LOG_GROUP_AUDIT_LOGIN = "Login";

    /**
     * 统计日志
     *
     * @param logger
     * @param cmdSign
     */
    public static void statLog(Logger logger, CmdSign cmdSign, String subject) {
        if (SysConstants.SERVER_ENV.equals(SysConstants.SERVER_ENV_PROD)) {
            JSONObject logCmdSign = buildBaseLog(cmdSign);
            logCmdSign.put("logGroup", LogUtils.LOG_GROUP_STAT);
            logCmdSign.put("logSubject", subject);
            if (cmdSign.getExeEndTime() != null && cmdSign.getExeStartTime() != null) {
                logCmdSign.put("exeTime", cmdSign.getExeEndTime() - cmdSign.getExeStartTime());
            }
            logger.info("LogJson:" + new Gson().toJson(logCmdSign));
        }
    }

    /**
     * 审计日志
     *
     * @param logger
     * @param cmdSign
     */
    public static void auditLog(Logger logger, CmdSign cmdSign, String bizNo, Object data) {
        if (SysConstants.SERVER_ENV.equals(SysConstants.SERVER_ENV_PROD)) {
            JSONObject logCmdSign = buildBaseLog(cmdSign);
            logCmdSign.put("logGroup", LogUtils.LOG_GROUP_AUDIT);
            logCmdSign.put("bizNo", bizNo);
            logCmdSign.put("token", cmdSign.getToken());
            logCmdSign.put("data", data);
            logger.info("LogJson:" + new Gson().toJson(logCmdSign));
        }
    }

    public static void auditLog(Logger logger, CmdSign cmdSign, String bizNo) {
        if (SysConstants.SERVER_ENV.equals(SysConstants.SERVER_ENV_PROD)) {
            JSONObject logCmdSign = buildBaseLog(cmdSign);
            logCmdSign.put("logGroup", LogUtils.LOG_GROUP_AUDIT);
            logCmdSign.put("bizNo", bizNo);
            logCmdSign.put("token", cmdSign.getToken());
            logCmdSign.put("data", cmdSign.getSource());
            logger.info("LogJson:" + new Gson().toJson(logCmdSign));
        }
    }

    public static void bdaLog(Logger logger, String data) {
        if (SysConstants.SERVER_ENV.equals(SysConstants.SERVER_ENV_PROD)) {
            if(StringUtils.isNotBlank(data)){
                logger.info("BdaJson:" + data);
            }
        }
    }

    public static void exLog(Logger logger, String data) {
        logger.info("ExJson:" + data);
    }

    private static JSONObject buildBaseLog(CmdSign cmdSign) {
        JSONObject logCmdSign = new JSONObject();
        logCmdSign.put("scope", cmdSign.getScope());
        logCmdSign.put("cmdCode", cmdSign.getCmdCode());
        logCmdSign.put("reqCode", cmdSign.getReqCode());
        logCmdSign.put("cmdVersion", cmdSign.getCmdVersion());
        logCmdSign.put("userId", cmdSign.getUserId());
        logCmdSign.put("token", cmdSign.getToken());
        return logCmdSign;
    }
}
